﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.BusinessLayer;

namespace XJX.Page
{
    /// <summary>
    /// </summary>
    public class SysMessage
    {
        public SysMessage()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        //获取一个家庭的所有信件信息
        public static string Get_One_Family_Message_From_UID(string UID, int PageIndex, int PageSize)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.SysMessage bs = new XJX.BusinessLayer.SysMessage(str);

            Hashtable ht = bs.Get_One_Family_Message_From_UID(UID, PageIndex, PageSize);
            return XJX.BusinessLayer.JSON.Encode(ht);
        }

        //获取一个家庭的所有未读信件数量
        public static string Get_One_Family_Message_From_UID(string UID)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.SysMessage bs = new XJX.BusinessLayer.SysMessage(str);

            DataTable dt = bs.Get_One_Family_Message_From_UID(UID);

            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["flag"].ToString() == "0")
                {
                    i++;
                }
            }
            return i.ToString();
        }


        //删除一条家庭信件
        public static bool Del_One_Family_Message_From_ID(string ID)
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.SysMessage bs = new XJX.BusinessLayer.SysMessage(str);

            try
            {
                b = bs.Del_One_Family_Message_From_ID(ID);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

        //添加新的站内信
        public static bool Add_New_SysMsg
            (
            string uid,
            string bid,
            string title,
            string msg,
            out string ReturnFieldValue   //返回字段值
            )
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.SysMessage bs = new XJX.BusinessLayer.SysMessage(str);

            try
            {
                b = bs.Add_New_SysMsg(uid,bid,title,msg,out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

        //添加未成功发送的短信消息
        public static bool Add_New_NoSuccessShortMsg
            (
            string SysMsgID,
            string TemplateID,
            string ClientIP
            )
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.SysMessage bs = new XJX.BusinessLayer.SysMessage(str);

            try
            {
                b = bs.Add_New_NoSuccessShortMsg(SysMsgID, TemplateID, ClientIP);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }


        //标记一条信息为已读
        public static bool Mark_One_SysMsg_Readed(string ID)
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.SysMessage bs = new XJX.BusinessLayer.SysMessage(str);

            try
            {
                b = bs.Mark_One_SysMsg_Readed(ID);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

    }

    ////////////////////////////////////////////////////////////////////////////以下系统信息类属性

    public class SysInfo
    {
        public string ID;
        public string UID;
        public string Bid;
        public string Title;
        public string Msg;
        public string Cdate;
        public string Flag;
    }

}